package com.training2.bookstore.order.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.training2.bookstore.log.model.LogRecord;
import com.training2.bookstore.log.repo.LogRepository;
import com.training2.bookstore.order.model.Cart;
import com.training2.bookstore.order.model.Order;
import com.training2.bookstore.order.service.OrderService;




//ORM

@RestController
@RequestMapping("/order")
public class OrderController {

@Autowired OrderService service;
@Autowired LogService logService;

	@RequestMapping(value = "add", method=RequestMethod.POST)
    @PreAuthorize("hasAuthority('USER')")
	public Order create(@RequestBody Cart cart, HttpServletRequest req) {
		
		logService.logIt(req,"order.create", "order/add");
		
		
		
		Order order = null;
		try {
			order = service.createOrder(cart);
		} catch (Exception e) {
			//........
			e.printStackTrace();
		}
		
		return order;
		
	}



	@GetMapping(value = "myorders")
	@PreAuthorize("hasAuthority('USER')")//ADMIN_USER USER
	public List<Order> myOrders( HttpServletRequest req) {
		
		
		logService.logIt(req,"order.myorders", "order/myorders");
		
		
		List<Order> orders = null;
		try {
			orders = service.myOrders();
		} catch (Exception e) {
			// .....
			e.printStackTrace();
		}
		
		return orders;
		
	}
}
